package com.adcc.ags.gms.mq.vo;

import com.adcc.ags.gms.core.vo.BaseVO;
import com.adcc.ags.gms.mq.entity.Queue;
import com.adcc.utility.mq.entity.ibm.IBMQueue;
import com.adcc.utility.mq.entity.ibm.LocalQueue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.ibm.mq.constants.CMQC;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 队列视图
 */
public class QueueVO extends BaseVO {

    private static final String COMPARE_MARK = "->";

    // ID
    private long id;

    // 名称
    private String name;

    // 类型
    private int type;

    // 类型名称
    private String typeName;

    // 持久化
    private boolean persistent;

    // 队列优先级
    private int priority;

    // 生产者数
    private long producers;

    // 消费者数
    private long consumers;

    // 队列深度
    private long depth;

    // 最大队列深度
    private int maxDepth;

    // 最大消息长度
    private int maxMsgLength;

    // 保留时间间隔
    private int retainInterval;

    // 创建时间
    private String createTime;

    // 是否生效
    private boolean enable;

    // 是否堵报
    private boolean blocked;

    // 对比结果
    private String compareResult;

    /**
     * 构造函数
     */
    public QueueVO() {
        super();
    }

    /**
     * 构造函数
     * @param queue
     */
    public QueueVO(Queue queue) {
        if(queue != null){
            id = queue.getId();
            name = queue.getName();
            type = queue.getType();
            typeName = formatTypeName(queue.getType());
            if(queue.getPersistent() == 1){
                persistent = true;
            }else{
                persistent = false;
            }
            priority = queue.getPriority();
            maxDepth = queue.getMaxDepth();
            maxMsgLength = queue.getMaxMsgLength();
            retainInterval = queue.getRetainInterval();
            createTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(queue.getCreateTime());
            if("T".equals(queue.getEnable())){
                enable = true;
            }else{
                enable = false;
            }
        }
    }

    /**
     * 构造函数
     * @param queue
     */
    public QueueVO(IBMQueue queue){
        if(queue != null){
            name = queue.getName();
            type = queue.getType();
            typeName = formatTypeName(type);
            persistent = queue.isPersistent();
            priority = queue.getPriority();
            producers = queue.getProducers();
            consumers = queue.getConsumers();
            depth = queue.getDepth();
            if(queue instanceof LocalQueue){
                maxDepth = ((LocalQueue)queue).getMaxDepth();
                maxMsgLength = ((LocalQueue)queue).getMaxMsgLength();
                retainInterval = ((LocalQueue)queue).getRetainInterval();
            }
        }
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public boolean getPersistent() {
        return persistent;
    }

    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public long getProducers() {
        return producers;
    }

    public void setProducers(long producers) {
        this.producers = producers;
    }

    public long getConsumers() {
        return consumers;
    }

    public void setConsumers(long consumers) {
        this.consumers = consumers;
    }

    public long getDepth() {
        return depth;
    }

    public void setDepth(long depth) {
        this.depth = depth;
    }

    public int getMaxDepth() {
        return maxDepth;
    }

    public void setMaxDepth(int maxDepth) {
        this.maxDepth = maxDepth;
    }

    public int getMaxMsgLength() {
        return maxMsgLength;
    }

    public void setMaxMsgLength(int maxMsgLength) {
        this.maxMsgLength = maxMsgLength;
    }

    public int getRetainInterval() {
        return retainInterval;
    }

    public void setRetainInterval(int retainInterval) {
        this.retainInterval = retainInterval;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public boolean isBlocked() {
        return blocked;
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }

    public String getCompareResult() {
        return compareResult;
    }

    public void setCompareResult(String compareResult){
        this.compareResult = compareResult;
    }

    private String formatTypeName(int type){
        String strTypeName = Strings.nullToEmpty("");
        switch (type){
            case CMQC.MQQT_LOCAL:
                strTypeName = "本地队列";
                break;
            case CMQC.MQQT_ALIAS:
                strTypeName = "别名队列";
                break;
            case CMQC.MQQT_MODEL:
                strTypeName = "模型队列";
                break;
            case CMQC.MQQT_REMOTE:
                strTypeName = "远程队列";
                break;
            default:
                strTypeName = "本地队列";
                break;
        }
        return  strTypeName;
    }

    public String compare(QueueVO vo){
        StringBuilder sb = new StringBuilder();
        if(vo != null){
            if(persistent != vo.getPersistent()){
                String strPersistent = (vo.getPersistent() ? "T" : "F") + COMPARE_MARK + (persistent ? "T" : "F");
                sb.append("persistent:" + strPersistent);
            }
            if(priority != vo.getPriority()){
                String strPriority = vo.getPriority() + COMPARE_MARK + priority;
                sb.append("priority:" + strPriority);
            }
            if(maxDepth != vo.getMaxDepth()){
                String strMaxDepth = vo.getMaxDepth() + COMPARE_MARK + maxDepth;
                sb.append("maxDepth:" + strMaxDepth);
            }
            if(maxMsgLength != vo.getMaxMsgLength()){
                String strMaxMsgLength = vo.getMaxMsgLength() + COMPARE_MARK + maxMsgLength;
                sb.append("maxMsgLength:" + strMaxMsgLength);
            }
            if(retainInterval != vo.getRetainInterval()){
                String strRetainInterval = vo.getRetainInterval() + COMPARE_MARK + retainInterval;
                sb.append("retainInterval:" + strRetainInterval);
            }
        }
        return sb.toString();
    }

    @Override
    public Queue toEntity() throws ParseException {
        Queue queue = new Queue();
        queue.setId(this.id);
        queue.setName(this.name);
        queue.setType(this.type);
        if(this.getPersistent()){
            queue.setPersistent(1);
        }else{
            queue.setPersistent(0);
        }
        queue.setPriority(this.priority);
        queue.setMaxDepth(this.maxDepth);
        queue.setMaxMsgLength(this.maxMsgLength);
        queue.setRetainInterval(this.retainInterval);
        queue.setCreateTime(new Date());
        if(enable) {
            queue.setEnable("T");
        }else{
            queue.setEnable("F");
        }
        return queue;
    }

    @Override
    public String toJson() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(this);
    }
}
